package com.example.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.admin.mapper.AdUserMapper;
import com.example.admin.pojo.AddUser;
import com.example.admin.pojo.LoginUser;
import com.example.admin.service.AdUserService;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.utils.common.AppJwtUtil;
import com.usian.utils.common.MD5Utils;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@Service
public class AdUserServiceImpl implements AdUserService {
    @Autowired
    AdUserMapper adUserMapper;

    @Override
    public ResponseResult addUser(AddUser addUser) {
        QueryWrapper<AdUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",addUser.getName());
        AdUser user = adUserMapper.selectOne(queryWrapper);
        if (user!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        AdUser adUser = new AdUser();
        BeanUtils.copyProperties(addUser,adUser);
        Random random = new Random();
        if (("").equals(adUser.getNickname())||adUser.getNickname()==null){
            int i = random.nextInt(99999999);
            if (i<10000000){
                i = i+1000;
            }
            adUser.setNickname("User_"+ i);
        }
        adUser.setStatus(0);
        adUser.setCreatedTime(new Date());
        String j = random.nextInt(1000000)+"yjy";
        adUser.setSalt(j);
        adUser.setPassword(MD5Utils.encodeWithSalt(addUser.getPassword(),j));
        adUserMapper.insert(adUser);
        return ResponseResult.okResult("注册成功");
    }

    @Override
    public ResponseResult login(LoginUser loginUser) {
        QueryWrapper<AdUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",loginUser.getName());
        List<AdUser> userList = adUserMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(userList) && userList.size()==1){
            AdUser adUser = userList.get(0);
            String password = MD5Utils.encodeWithSalt(loginUser.getPassword(), adUser.getSalt());
            if (password.equals(adUser.getPassword())){
//                userList.get(0).setUserLastTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
//                userMapper.updateById(userList.get(0));
                HashMap hashMap = new HashMap();
                String token = AppJwtUtil.getToken(Long.valueOf(adUser.getId()));
                adUser.setSalt("");
                adUser.setPassword("");
                hashMap.put("adUser",adUser);
                hashMap.put("token",token);
                return ResponseResult.okResult(hashMap);
            }
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
    }
}
